using System;
using System.Collections.Generic;
using System.Text;
using LispInterpretor.HelperClasses;

namespace LispInterpretor.Parsers
{
    public class DefineParser : IParser
    {
        #region IParser Members

        public bool Is(string text, Environment environment)
        {
            try
            {
                string result = ParserHelper.ParseFuncName(text);

                return string.Compare(result, "def") == 0;
            }
            catch (InvalidProgramException)
            {
                return false;
            }
        }

        public object Evaluate(string text, Environment environment)
        {
            IList<string> arguments = ParserHelper.ParseArguments(text);

            Check.IsTrue(arguments.Count == 3, "def requires exactly two arguments.");

            environment.Add(arguments[1], Evaluator.Eval(arguments[2], environment));

            return null;
        }

        #endregion
    }
}
